package com.zwj.web;

import com.mysql.cj.Session;
import com.zwj.bean.Book;
import com.zwj.bean.Page;
import com.zwj.service.BookService;
import com.zwj.service.Impl.BookServiceImpl;
import com.zwj.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zwj
 * @create 2021-05-14-14:58
 */
public class ClientBookServlet extends BaseServlet{
    private BookService bookService = new BookServiceImpl();

    /**
     * 客户端显示图书信息，通过web下的index请求访问
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //System.out.println("clientservlet");
        //获取请求参数
        int pageNoInt = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSizeInt = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //获取页面显示对象
        Page<Book> page = bookService.page(pageNoInt, pageSizeInt);
        page.setUrl("client/bookServlet?action=page");
        //存入域中
        req.setAttribute("page",page);
        //请求通过web下的index过来，存好数据后请求跳转去/pages/client/index.jsp来显示
        req.getRequestDispatcher("/pages/client/index.jsp").forward(req,resp);
    }

    /**
     * 客户端根据价格显示图书信息,通过client/index的form表单提交访问
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void pageByPrice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //System.out.println("clientservlet");
        //获取请求参数
        int pageNoInt = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSizeInt = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        int min = WebUtils.parseInt(req.getParameter("min"), 0);
        int max = WebUtils.parseInt(req.getParameter("max"), Integer.MAX_VALUE);
        //获取页面显示对象
        Page<Book> page = bookService.pageByPrice(pageNoInt, pageSizeInt,min,max);
        page.setUrl("client/bookServlet?action=pageByPrice");
        //存入域中
        req.setAttribute("page",page);
        //请求通过client下的价格区间书籍查询过来，存好数据后请求跳转回来显示
        req.getRequestDispatcher("/pages/client/index.jsp").forward(req,resp);
    }
}
